package com.niodata.dp.core.security;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ServiceAuthMapper {

  @Insert("REPLACE into SERVICE_AUTH(SER_NAME,TARGET_ID,TARGET_TYPE,AUTH_TIME)"
        + "VALUES(#{auth.service},#{auth.targetId},#{auth.targetType},#{auth.authTime})")
  void addServiceAuth(@Param("auth") ServiceAuth auth);

  @Select("select * from SERVICE_AUTH where TARGET_ID=#{targetId} and TARGET_TYPE=#{targetType}")
  @ResultMap("serviceAuthRestMap")
  List<ServiceAuth> getServiceAuthList(@Param("targetId") long targetId,
        @Param("targetType") String targetType);

  @Delete("delete from SERVICE_AUTH where SER_NAME=#{name} and TARGET_ID=#{targetId} and "
        + "TARGET_TYPE=#{targetType}")
  void deleteAuth(@Param("name") String name, @Param("targetId") long targetId,
        @Param("targetType") String targetType);

  @Delete("delete from SERVICE_AUTH where SER_NAME=#{name}")
  void deleteAuthOfService(@Param("name") String name);

}
